﻿#include "ImageLabel.h"

ImageLabel::ImageLabel(QWidget *parent) : QLabel(parent) {
    setAlignment(Qt::AlignCenter);
    setMinimumSize(400, 300);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

void ImageLabel::loadImage(const QString &filePath) {
    originalImage.load(filePath);
    if (!originalImage.isNull()) {
        setPixmap(QPixmap::fromImage(originalImage));
        // points.clear();
        update();
    }
}

void ImageLabel::saveImage(const QString &filePath) {
    if (originalImage.isNull()) return;

    // 创建带标记的图像副本
    QImage imageWithMarkers = originalImage.copy();
    QPainter painter(&imageWithMarkers);
    painter.setRenderHint(QPainter::Antialiasing);

    // // 设置标记样式
    // QFont font;
    // font.setPointSize(16);
    // font.setBold(true);
    // painter.setFont(font);

    // // 绘制所有标记点
    // for (int i = 0; i < points.size(); ++i) {
    //     const QPoint &point = points[i];

    //     // 绘制圆形
    //     painter.setPen(QPen(Qt::red, 3));
    //     painter.setBrush(QBrush(QColor(255, 100, 100, 150))); // 半透明填充
    //     painter.drawEllipse(point, 15, 15);

    //     // 绘制序号
    //     painter.setPen(Qt::white);
    //     QString number = QString::number(i + 1);

    //     // 计算文本位置使其居中
    //     QFontMetrics metrics(font);
    //     int textWidth = metrics.horizontalAdvance(number);
    //     int textHeight = metrics.height();

    //     painter.drawText(point.x() - textWidth/2, point.y() + textHeight/4, number);
    // }

    // painter.end();

    if (imageWithMarkers.save(filePath)) {
        emit sigSaveSuccessed(filePath);
        QMessageBox::information(this, "保存成功", "图片已成功保存!");
    } else {
        QMessageBox::warning(this, "保存失败", "无法保存图片!");
    }
}

void ImageLabel::clearMarkers() {
    points.clear();
    update();
}

QPoint ImageLabel::convertToImageCoordinates(const QPoint &widgetPos) {
    if (originalImage.isNull()) return widgetPos;

    // 获取QLabel中图像的实际显示区域
    QPixmap currentPixmap = pixmap(Qt::ReturnByValue);
    if (currentPixmap.isNull()) return widgetPos;

    QRect contentRect = contentsRect();
    QSize pixmapSize = currentPixmap.size();

    // 计算图像在QLabel中的实际位置和缩放比例
    double xScale = static_cast<double>(originalImage.width()) / pixmapSize.width();
    double yScale = static_cast<double>(originalImage.height()) / pixmapSize.height();

    // 计算图像在QLabel中的显示区域
    QRect imageRect;
    imageRect.setSize(pixmapSize);
    imageRect.moveCenter(contentRect.center());

    // 转换为图像坐标
    int x = (widgetPos.x() - imageRect.left()) * xScale;
    int y = (widgetPos.y() - imageRect.top()) * yScale;

    return QPoint(x, y);
}

QPoint ImageLabel::convertToWidgetCoordinates(const QPoint &imagePos) {
    if (originalImage.isNull()) return imagePos;

    // 获取QLabel中图像的实际显示区域
    QPixmap currentPixmap = pixmap(Qt::ReturnByValue);
    if (currentPixmap.isNull()) return imagePos;

    QRect contentRect = contentsRect();
    QSize pixmapSize = currentPixmap.size();

    // 计算图像在QLabel中的实际位置和缩放比例
    double xScale = static_cast<double>(pixmapSize.width()) / originalImage.width();
    double yScale = static_cast<double>(pixmapSize.height()) / originalImage.height();

    // 计算图像在QLabel中的显示区域
    QRect imageRect;
    imageRect.setSize(pixmapSize);
    imageRect.moveCenter(contentRect.center());

    // 转换为窗口坐标
    int x = imageRect.left() + imagePos.x() * xScale;
    int y = imageRect.top() + imagePos.y() * yScale;

    return QPoint(x, y);
}
